﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using YellowMap.View.Control;
using System.Windows.Media;
using YellowMap.Resources;

namespace YellowMap.View.Page
{
    public partial class HistoryPage : PhoneApplicationPage
    {
        /*
       * [vttrung]: FT06
       */
        public static HistoryPage Intance { get; set; }
        public HistoryPage()
        {
            InitializeComponent();
            Intance = this;
            ReloadLanguage(AppViewModel.Instance.SettingVM.Language);
            Init();
            LoadTheme();
        }

        // Detect Theme of History screen and Load theme to app
        private void LoadTheme()
        {
            TitHistory.Foreground = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_Text);
            (ApplicationBar as ApplicationBar).BackgroundColor = AppViewModel.Instance.SettingVM.BGColor_Appbar;
            if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] != Visibility.Visible)
            {
                //ImgBrush.ImageSource = null;
                LayoutRoot.Background = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_screen);
            }
        }
        // Load Language when change language in settings screen
        private void ReloadLanguage(string languageName)
        {
            switch (languageName)
            {
                case "Vi":
                    TitHistory.Text = LangVi.str_history;
                    //btnView.Header = LangVi.str_view;
                    //btnSearch.Header = LangVi.str_search;
                    (ApplicationBar.Buttons[0] as ApplicationBarIconButton).Text = LangVi.str_deleteall;
                    break;
                case "En":
                    TitHistory.Text = LangEn.str_history;
                    //btnView.Header = LangEn.str_view;
                    //btnSearch.Header = LangEn.str_search;
                    (ApplicationBar.Buttons[0] as ApplicationBarIconButton).Text = LangEn.str_deleteall;
                    break;
            }
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            SearchPage.isFromSearchPage = false;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ReloadLanguage(AppViewModel.Instance.SettingVM.Language);
            base.OnNavigatedTo(e);
        }


        // set Item source listbox history
        async void Init(string id = "")
        {
            await UpdateDB(id);
            lbHistory.ItemsSource = AppViewModel.Instance.HistoryVM.Histories;
            if (lbHistory.Items.Count > 0)
            {
                (ApplicationBar.Buttons[0] as ApplicationBarIconButton).IsEnabled = true;
            }
            else
                (ApplicationBar.Buttons[0] as ApplicationBarIconButton).IsEnabled = false;
        }

        // Update data base if have change
        async Task UpdateDB(string id = "")
        {
            await AppViewModel.Instance.HistoryVM.Update();
        }
        // delete all history items
        async void DeleteHistory()
        {
            await DBHelper.ClearAllHistory();
            Init("");
        }
        // Handle delete all button
        private void DeleteAll_Click(object sender, EventArgs e)
        {
            DeleteHistory();
        }

        //Handle View button
        private void btnView_Click(object sender, EventArgs e)
        {
            AppViewModel.Instance.SettingVM.SelectedHistory = HistoryListItem.PressedItem.DataContext as History;
            MainPage.COMMAND = MainPage_Command.SHOW_HISTORY;
            if (MainPage.Instance.tbSearch.Visibility == System.Windows.Visibility.Visible)
            {
                MainPage.Instance.tbSearch.Visibility = System.Windows.Visibility.Collapsed;
                MainPage.Instance.titleBG.Visibility = System.Windows.Visibility.Collapsed;
                MainPage.Instance.imagesearch.Visibility = System.Windows.Visibility.Collapsed;
            }
            if ((MainPage.Instance.ApplicationBar.Buttons[3] as ApplicationBarIconButton).IsEnabled == false)
            {
                (MainPage.Instance.ApplicationBar.Buttons[3] as ApplicationBarIconButton).IsEnabled = true;
            }
            SearchPage.isViewHistory = true;
            NavigationService.GoBack();
        }

        //Handle search button
        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            //MainPage.SelectedHistory = lbHistory.SelectedItem as History;
            AppViewModel.Instance.SettingVM.SelectedHistory = HistoryListItem.PressedItem.DataContext as History;
            MainPage.COMMAND = MainPage_Command.SEARCH_HISTORY;
            if (MainPage.Instance.tbSearch.Visibility == System.Windows.Visibility.Visible)
            {
                MainPage.Instance.tbSearch.Visibility = System.Windows.Visibility.Collapsed;
                MainPage.Instance.titleBG.Visibility = System.Windows.Visibility.Collapsed;
                MainPage.Instance.imagesearch.Visibility = System.Windows.Visibility.Collapsed;
            }
            if ((MainPage.Instance.ApplicationBar.Buttons[3] as ApplicationBarIconButton).IsEnabled == false)
            {
                (MainPage.Instance.ApplicationBar.Buttons[3] as ApplicationBarIconButton).IsEnabled = true;
            }
            SearchPage.isSearchHistory = true;
            NavigationService.GoBack();
        }
    }
}